import { getTypeList, getProList } from '@/api'

export default {
  namespaced: true,
  state: {
    listType: [],
    listTotal: [],
    listNowUse: [],
    listNowCheck: ''
  },
  mutations: {
    SET_LISTTYPE: (state, data) => {
      state.listType = data
    },
    SET_LISTTOTA: (state, data) => {
      state.listTotal = data
    },
    SET_LISTNOWUSE: (state, data) => {
      state.listNowUse = data
    },
    SET_LISTNOWCHECK: (state, data) => {
      state.listNowCheck = data
    }
  },
  actions: {
    getListType({ commit }) {
      return new Promise((resolve, reject) => {
        getTypeList().then(res => {
          const { data } = res
          commit('SET_LISTTYPE', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getListTotal({ commit }) {
      return new Promise((resolve, reject) => {
        getProList().then(res => {
          const { data } = res
          commit('SET_LISTTOTA', data)
          commit('SET_LISTNOWUSE', data)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    getListNowUse({ commit, state }, type) {
      return new Promise((resolve, reject) => {
        const data = state.listTotal.filter(obj => obj.type === type)
        console.log(data, type)
        commit('SET_LISTNOWCHECK', type)
        commit('SET_LISTNOWUSE', data)
        resolve()
      })
    }
  }
}
